package com.fortune.controller;

import java.util.List;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.PaymentMgtExcution;
import com.fortune.dto.in.paymentMgt.DistributorPaymentListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.PaymentMgtEnum;
import com.fortune.service.PaymentMgtService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.paymentMgt.DidPayment;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.paymentMgt.DistributorPaymentAddDto;
import com.fortune.dto.in.paymentMgt.DistributorPaymentEditDto;
import com.fortune.vo.paymentMgt.DistributorPayment;
import com.fortune.vo.paymentMgt.DistributorPaymentDetail;
import com.fortune.vo.paymentMgt.MOutInfoInPaymentMgt;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "paymentMgt", description = "经销商回款单管理", tags = {"经销商回款单管理"})
@RestController
@RequestMapping("paymentMgt")
public class PaymentMgtController {
	@Autowired
	private PaymentMgtService service;

	@ApiOperation(value = "汇款单列表", nickname = "paymentMgtList", notes = "", tags={ "@安童" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
			@ApiImplicitParam(name="approved", value="批准筛选",paramType="query",dataType="int"),
			@ApiImplicitParam(name="isDealer", value="经销商/厂商",paramType="query",dataType="int"),
			@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData> list(HttpServletRequest request, @Validated @ApiIgnore DistributorPaymentListDto dto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		PagingData pagingData = new PagingData();
		dto.setPage((dto.getPage()-1)*10);
		List<DistributorPayment> list = service.findDistributorPaymentList(dto,mid);
		int count = service.findDistributorPaymentListCount(dto,mid);
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new PaymentMgtExcution<PagingData>(pagingData, PaymentMgtEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "汇款单详情", nickname = "paymentMgtDetail", notes = "", tags={ "@安童" })
	@ApiImplicitParam(name="paymentId", value="汇款单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DistributorPaymentDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="汇款单id")String paymentId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		DistributorPaymentDetail detail = service.findDetail(mid,paymentId);
		return new PaymentMgtExcution<DistributorPaymentDetail>(detail,PaymentMgtEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "新增汇款单", nickname = "paymentMgtAdd", notes = "", tags={ "@安童" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DistributorPaymentAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		String userId = mUserInfo.getId();
		String id = UUIDUtil.get32UUID();
		int num = service.add(userId,addDto,mid,id);
		if(num > 0){
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.ADD_SUCCESS);
		}
		return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.ADD_FALSE);
	}
	
	@ApiOperation(value = "编辑汇款单", nickname = "paymentMgtEdit", notes = "", tags={ "@安童" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DistributorPaymentEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		String userId = mUserInfo.getId();
		int num = service.update(userId,editDto,mid,editDto.getDid());
		if(num > 0){
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.EDIT_SUCCESS);
		}
		return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.EDIT_FALSE);
	}
	
	@ApiOperation(value = "获取出库单列表", nickname = "paymentMgtMOutlist", notes = "", tags={ "@安童" })
	@ApiImplicitParam(name="distributorId", value="经销商id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "mOutlist",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MOutInfoInPaymentMgt>> mOutlist(HttpServletRequest request,@Validated @NotBlank(message="经销商id不能为空")String distributorId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		List<MOutInfoInPaymentMgt> list = service.findOutList(mid,distributorId);
		return new PaymentMgtExcution<List<MOutInfoInPaymentMgt>>(list,PaymentMgtEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "编辑获取出库单列表", nickname = "paymentMgtMEditOutlist", notes = "", tags={ "@安童" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="distributorId", value="经销商id",paramType="query",dataType="string",required=true),
			@ApiImplicitParam(name="id", value="付款单ID",paramType="query",dataType="string",required=true)
	})
	@GetMapping(value = "mEditOutlist",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MOutInfoInPaymentMgt>> mEditOutlist(HttpServletRequest request,@Validated @NotBlank(message="经销商id不能为空")String distributorId,@NotBlank(message="付款单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		List<MOutInfoInPaymentMgt> list = service.findOutListWhereId(mid,distributorId,id);
		return new PaymentMgtExcution<List<MOutInfoInPaymentMgt>>(list,PaymentMgtEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "删除汇款单", nickname = "paymentMgtDelete", notes = "", tags={ "@安童" })
	@ApiImplicitParam(name="paymentId", value="汇款单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="汇款单id")String paymentId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		int num = service.deletePayment(mid,paymentId);
		if(num > 0){
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.DELETE_SUCCESS);
		}else{
			return new PaymentMgtExcution<Void>(null,PaymentMgtEnum.DELETE_FALSE);
		}
	}

	@ApiOperation(value = "查所有经销商", nickname = "findDid", notes = "", tags={ "@安童" })
	@GetMapping(value = "findDid",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DidPayment>> findDid(HttpServletRequest request) {
		MUserInfo mUserInfo = (MUserInfo) request.getSession().getAttribute(Constants.SESSION_M_USER);
		List<DidPayment> list = service.findDid(mUserInfo.getmId());
		return new PaymentMgtExcution<List<DidPayment>>(list, PaymentMgtEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "查经销商经手人", nickname = "findDidUser", notes = "", tags={ "@安童" })
	@ApiImplicitParam(name="did", value="经销商id",paramType="query",dataType="string",required=true)
	@GetMapping(value = "findDidUser",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DidPayment>> findDidUser(@Validated @NotBlank(message="经销商ID不能为空")String did) {
		List<DidPayment> list = service.findDidUser(did);
		return new PaymentMgtExcution<List<DidPayment>>(list, PaymentMgtEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "查付款方式", nickname = "findPayType", notes = "", tags={ "@安童" })
	@GetMapping(value = "findPayType",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DidPayment>> findPayType() {
		List<DidPayment> list = service.findPayType();
		return new PaymentMgtExcution<List<DidPayment>>(list, PaymentMgtEnum.FIND_SUCCESS);
	}
}
